﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NTTHAO.Common;
using NTTHAO.Web.BL.Product;
using NTTHAO.CommonWeb;
using NTTHAO.Common.BL;
using System.IO;

public partial class admin_webparts_Plug_ins_Product_ProductCopy : System.Web.UI.UserControl
{
    public int _Step = 1;
    public ProductType _Type = null;
    //public ProductTypeConfig _Config = null;
    public DataTable _Products = null;
    public string _SelectedItems = "";
    public ProductGroup _Group = null;
    public int _CopyToTypeID = 0;
    public string _ProductUploadeFolder = "../" + Constant.UPLOAD_FILE_PATH;
    public int _Method = 0; // copy

    public string _MethodName
    {
        get
        {
            return (_Method == 0) ? "Sao chép" : "Di chuyển";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            btnCopyFrom.Text = btnCopyFrom2.Text = "Tiếp tục";
            

            int typeID = 0;
            Int32.TryParse(Request.QueryString["typeid"].Trim(), out typeID);
            if (typeID < 1)
            {
                return;
            }
            this._Type = ProductType.GetItem(typeID);
            
            DataSet ds = Product.GetAllByTypeID(_Type.GroupID, this._Type.ID, -1);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                this._Products = ds.Tables[0];
            }

            if (!IsPostBack)
            {
                Language lang = new Language();
                ds = lang.GetAll();
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    cmbLaguage.DataSource = ds.Tables[0];
                    cmbLaguage.DataTextField = Language.NAME__COLUMN_NAME;
                    cmbLaguage.DataValueField = Language.ID__COLUMN_NAME;
                    cmbLaguage.SelectedValue = LanguageUtility.GetLanguageID(Request, Session).ToString();

                    cmbLaguage.DataBind();
                }
            }

            _SelectedItems = Request.Params["chkId"] == null ? string.Empty : Request.Params["chkId"];
            _Method = Request.Params["rdMethod"] == null ? 0 : (Request.Params["rdMethod"] == "1" ? 1 : 0);

            if (Request.QueryString["step"] != null)
            {
                _SelectedItems = Request.QueryString["chkId"];
                Int32.TryParse(Request.QueryString["step"], out _Step);
                Int32.TryParse(Request.QueryString["totype"], out _CopyToTypeID);
                Int32.TryParse(Request.QueryString["rdMethod"], out _Method);
            }

            if (_Step == 5)
            {
                if (_Method == 0)
                {
                    this.CopyAll();
                }
                else
                {
                    this.MoveAll();
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message + "<br />" + ex.StackTrace);
        }
    }

    protected void MoveAll()
    {
        try
        {
            string[] proIDListStr = _SelectedItems.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
            for (int i = proIDListStr.Length - 1; i >= 0; i--)
            {
                int productID = 0;
                Int32.TryParse(proIDListStr[i], out productID);
                Product pro = new Product(productID);
                if (pro.ID > 0)
                {
                    pro.TypeID = _CopyToTypeID;
                    pro.Update();
                }
            }
            
            //int[] proIDListInt = new Int32[proIDListStr.Length];
            //for (int i = 0; i < proIDListStr.Length; i++)
            //{
            //    if (!Int32.TryParse(proIDListStr[i], out proIDListInt[i]))
            //    {
            //        proIDListInt[i] = 0;
            //    }
            //}

            //DataSet ds = Product.GetProductsByIDs(proIDListInt);
            //if (ds != null && ds.Tables[0].Rows.Count > 0)
            //{
            //    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            //    {
            //        int productID = (int)ds.Tables[0].Rows[i][Product.ID__COLUMN_NAME];
            //        Product pro = new Product(productID);
            //        if (pro.ID > 0)
            //        {
            //            pro.TypeID = _Type.ID;
            //            pro.Update();
            //        }
            //    }
            //}
        }
        catch { }
    }

    protected void CopyAll()
    {
        try
        {
            string[] proIDListStr = _SelectedItems.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
            int[] proIDListInt = new Int32[proIDListStr.Length];
            for (int i = 0; i < proIDListStr.Length; i++)
            {
                if (!Int32.TryParse(proIDListStr[i], out proIDListInt[i]))
                {
                    proIDListInt[i] = 0;
                }
            }

            DataSet ds = Product.GetProductsByIDs(proIDListInt);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
                {
                    int productID = (int)ds.Tables[0].Rows[i][Product.ID__COLUMN_NAME];
                    Product pro = new Product(productID);
                    if (pro.ID > 0)
                    {
                        Product proCopy = pro.Clone();

                        proCopy.TypeID = _CopyToTypeID;
                        proCopy.Image = string.Empty;
                        proCopy.Image_1 = string.Empty;
                        proCopy.Image_2 = string.Empty;
                        proCopy.Image_3 = string.Empty;
                        proCopy.Image_4 = string.Empty;
                        proCopy.Image_5 = string.Empty;
                        proCopy.Image_6 = string.Empty;
                        proCopy.Image_7 = string.Empty;
                        proCopy.Image_8 = string.Empty;
                        proCopy.Image_9 = string.Empty;
                        proCopy.Image_10 = string.Empty;

                        proCopy.Insert();
                        proCopy.ImagePath = "product/" + DateTime.Now.ToString("yyyy/MM") + "/" + proCopy.ID.ToString("0") + "/";
                        this.CopyProductImages(pro, ref proCopy);
                        proCopy.Update();
                    }
                }
            }
        }
        catch { }
    }
    
    public void CopyProductImages(Product source, ref Product des)
    {
        string srcFilePath = _ProductUploadeFolder + source.ImagePath;
        string desFilePath = _ProductUploadeFolder + des.ImagePath;

        if (!Directory.Exists(Server.MapPath(desFilePath)))
        {
            Directory.CreateDirectory(Server.MapPath(desFilePath));
        }
        string srcFileName = string.Empty;
        string desFileName = string.Empty;
        int index = 0;

        // 0 
        if (source.Image.Length > 0)
        {
            index = 0;
            des.Image = des.UrlName + "-" + des.ID.ToString() + "_" + index.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(source.Image);
            srcFileName = Server.MapPath(srcFilePath + source.Image);
            desFileName = Server.MapPath(desFilePath + des.Image);

            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
            srcFileName = Server.MapPath(srcFilePath + "Mini_" + source.Image);
            desFileName = Server.MapPath(desFilePath + "Mini_" + des.Image);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
        }

        // 1
        if (source.Image_1.Length > 0)
        {
            index = 1;
            des.Image_1 = des.UrlName + "-" + des.ID.ToString() + "_" + index.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(source.Image_1);
            srcFileName = Server.MapPath(srcFilePath + source.Image_1);
            desFileName = Server.MapPath(desFilePath + des.Image_1);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
            srcFileName = Server.MapPath(srcFilePath + "Mini_" + source.Image_1);
            desFileName = Server.MapPath(desFilePath + "Mini_" + des.Image_1);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
        }

        // 2 
        if (source.Image_2.Length > 0)
        {
            index = 2;
            des.Image_2 = des.UrlName + "-" + des.ID.ToString() + "_" + index.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(source.Image_2);
            srcFileName = Server.MapPath(srcFilePath + source.Image_2);
            desFileName = Server.MapPath(desFilePath + des.Image_2);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
            srcFileName = Server.MapPath(srcFilePath + "Mini_" + source.Image_2);
            desFileName = Server.MapPath(desFilePath + "Mini_" + des.Image_2);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
        }

        // 3 
        if (source.Image_3.Length > 0)
        {
            index = 3;
            des.Image_3 = des.UrlName + "-" + des.ID.ToString() + "_" + index.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(source.Image_3);
            srcFileName = Server.MapPath(srcFilePath + source.Image_3);
            desFileName = Server.MapPath(desFilePath + des.Image_3);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
            srcFileName = Server.MapPath(srcFilePath + "Mini_" + source.Image_3);
            desFileName = Server.MapPath(desFilePath + "Mini_" + des.Image_3);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
        }

        // 4
        if (source.Image_4.Length > 0)
        {
            index = 4;
            des.Image_4 = des.UrlName + "-" + des.ID.ToString() + "_" + index.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(source.Image_4);
            srcFileName = Server.MapPath(srcFilePath + source.Image_4);
            desFileName = Server.MapPath(desFilePath + des.Image_4);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
            srcFileName = Server.MapPath(srcFilePath + "Mini_" + source.Image_4);
            desFileName = Server.MapPath(desFilePath + "Mini_" + des.Image_4);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
        }

        // 5 
        if (source.Image_5.Length > 0)
        {
            index = 5;
            des.Image_5 = des.UrlName + "-" + des.ID.ToString() + "_" + index.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(source.Image_5);
            srcFileName = Server.MapPath(srcFilePath + source.Image_5);
            desFileName = Server.MapPath(desFilePath + des.Image_5);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
            srcFileName = Server.MapPath(srcFilePath + "Mini_" + source.Image_5);
            desFileName = Server.MapPath(desFilePath + "Mini_" + des.Image_5);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
        }

        // 6 
        if (source.Image_6.Length > 0)
        {
            index = 6;
            des.Image_6 = des.UrlName + "-" + des.ID.ToString() + "_" + index.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(source.Image_6);
            srcFileName = Server.MapPath(srcFilePath + source.Image_6);
            desFileName = Server.MapPath(desFilePath + des.Image_6);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
            srcFileName = Server.MapPath(srcFilePath + "Mini_" + source.Image_6);
            desFileName = Server.MapPath(desFilePath + "Mini_" + des.Image_6);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
        }

        // 7 
        if (source.Image_7.Length > 0)
        {
            index = 7;
            des.Image_7 = des.UrlName + "-" + des.ID.ToString() + "_" + index.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(source.Image_7);
            srcFileName = Server.MapPath(srcFilePath + source.Image_7);
            desFileName = Server.MapPath(desFilePath + des.Image_7);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
            srcFileName = Server.MapPath(srcFilePath + "Mini_" + source.Image_7);
            desFileName = Server.MapPath(desFilePath + "Mini_" + des.Image_7);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
        }

        // 8 
        if (source.Image_8.Length > 0)
        {
            index = 8;
            des.Image_8 = des.UrlName + "-" + des.ID.ToString() + "_" + index.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(source.Image_8);
            srcFileName = Server.MapPath(srcFilePath + source.Image_8);
            desFileName = Server.MapPath(desFilePath + des.Image_8);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
            srcFileName = Server.MapPath(srcFilePath + "Mini_" + source.Image_8);
            desFileName = Server.MapPath(desFilePath + "Mini_" + des.Image_8);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
        }

        // 9 
        if (source.Image_9.Length > 0)
        {
            index = 9;
            des.Image_9 = des.UrlName + "-" + des.ID.ToString() + "_" + index.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(source.Image_9);
            srcFileName = Server.MapPath(srcFilePath + source.Image_9);
            desFileName = Server.MapPath(desFilePath + des.Image_9);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
            srcFileName = Server.MapPath(srcFilePath + "Mini_" + source.Image_9);
            desFileName = Server.MapPath(desFilePath + "Mini_" + des.Image_9);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
        }

        // 10 
        if (source.Image_10.Length > 0)
        {
            index = 10;
            des.Image_10 = des.UrlName + "-" + des.ID.ToString() + "_" + index.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(source.Image_10);
            srcFileName = Server.MapPath(srcFilePath + source.Image_10);
            desFileName = Server.MapPath(desFilePath + des.Image_10);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
            srcFileName = Server.MapPath(srcFilePath + "Mini_" + source.Image_10);
            desFileName = Server.MapPath(desFilePath + "Mini_" + des.Image_10);
            if (File.Exists(srcFileName))
            {
                File.Copy(srcFileName, desFileName);
            }
        }
    }

    protected void btnCopyFrom_Click(object sender, EventArgs e)
    {
        _Step = 2;

        if (cmbLaguage.Items.Count == 1)
        {
            cmbLaguage.SelectedIndex = 0;
            btnSite_Click(btnSite, null);
        }
    }
    
    protected void btnSite_Click(object sender, EventArgs e)
    {
        try
        {
            int langID = 0;
            Int32.TryParse(cmbLaguage.SelectedValue, out langID);
            int groupID = this.GetDefaultProductGroup(langID);
            _Group = new ProductGroup(groupID);
        }
        catch { }
        _Step = 3;
    }

    public int GetDefaultProductGroup(int langID)
    {
        DataSet ds = ProductGroup.GetAllByLanguageID(langID);

        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            return (int)ds.Tables[0].Rows[0][ProductGroup.ID__COLUMN_NAME];
        }

        return -1;
    }
}
